package com.fxn.pixsample;

import com.fxn.pix.Pix;
import com.fxn.modals.Img;
import com.fxn.modals.Options;
import com.fxn.adapters.MyAdapter;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.ListContainer;
import ohos.agp.components.TableLayoutManager;
import ohos.agp.components.Component;
import ohos.agp.utils.Color;
import ohos.bundle.AbilityInfo;
import ohos.utils.net.Uri;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;

/**
 * description : MainAbilitySlice
 *
 * @since 2021-04-15
 */
public class MainAbilitySlice extends AbilitySlice {
    private static final String TAG = "MainAbilitySlice";

    private ListContainer mListContainer;
    private Button addBtn;
    private ArrayList<Img> images = new ArrayList();
    private MyAdapter mMyAdapter;
    private Options options = null;

    /**
     * onStart
     *
     * @param intent intent
     */
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        getWindow().setStatusBarColor(Color.getIntColor("#075e54"));
        initView();
    }

    private void initView() {
        if (findComponentById(ResourceTable.Id_list_container) instanceof ListContainer) {
            mListContainer = (ListContainer) findComponentById(ResourceTable.Id_list_container);
        }
        mMyAdapter = new MyAdapter(this, new ArrayList<>());
        TableLayoutManager layoutManager = new TableLayoutManager();
        layoutManager.setColumnCount(2);
        mListContainer.setLayoutManager(layoutManager);
        mListContainer.setItemProvider(mMyAdapter);
        if (findComponentById(ResourceTable.Id_add_btn) instanceof Button) {
            addBtn = (Button) findComponentById(ResourceTable.Id_add_btn);
        }
        addBtn.setClickedListener(view -> {
            Pix.start(this, options);
        });
        mListContainer.setItemClickedListener(new MyItemClickedListener());
        initData();
    }

    private class MyItemClickedListener implements ListContainer.ItemClickedListener {

        @Override
        public void onItemClicked(ListContainer listContainer, Component component, int position, long longValue) {
            Img img = images.get(position);
            if (img != null) {
                Intent intent = new Intent();
                intent.setAction("android.intent.action.VIEW");
                try {
                    intent.setUriAndType(Uri.parse(img.getUrl()),
                            Files.probeContentType(new File(img.getUrl()).toPath()));
                    intent.addFlags(Intent.FLAG_NOT_OHOS_COMPONENT);
                    startAbility(intent);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private void initData() {
        options = Options.init()
                .setRequestCode(100)
                .setCount(5)
                .setFrontfacing(false)
                .setMode(Options.Mode.ALL)
                .setSpanCount(4)
                .setVideoDurationLimitinSeconds(10)
                .setScreenOrientation(AbilityInfo.DisplayOrientation.PORTRAIT)
                .setPath("pix/akshay");
    }

    @Override
    protected void onAbilityResult(int requestCode, int resultCode, Intent resultData) {
        super.onAbilityResult(requestCode, resultCode, resultData);
        if (requestCode == options.getRequestCode()) {
            if (resultData == null) {
                return;
            }
            images = resultData.getSequenceableArrayListParam(Pix.IMAGE_RESULTS);
            ArrayList<String> uriList = new ArrayList<>();
            for (Img img : images) {
                if ("".equals(img.getContentUrl()) || img.getContentUrl() == null) {
                    uriList.add(img.getUrl());
                } else {
                    uriList.add(img.getContentUrl());
                }
            }
            options.setPreSelectedUrls(uriList);
            mListContainer.setItemProvider(new MyAdapter(this, images));
        }
    }
}
